'''
计算投票，增加分数权重
'''
import random
def get_random(rate):
    i = random.randint(-1000000000,100000000)%100
    if i<(100*rate):
        return 1
    else:
        return 0

def init(people,rate):
    a=[]     #投票结果
    for i in range(people):
        a.append(get_random(rate[i]))    #根据比例生成随机数
    return a



def vote(Temptation, people, round, growth, score):
    scores = [score] * people
    rate = [Temptation]*people     #初始每个人的投票率
    a=init(people,rate)    #第一轮投票
    result = []    #要输出的所有投票率
    for i in range(len(a)):
        if a[i] == 1:  # 投票
            rate[i] = rate[i] + growth
            if scores[i] + growth * 100<=1:
                scores[i] = scores[i] + growth * 100
        else:
            rate[i] = rate[i] - growth
            if  scores[i] - growth * 100>=0:
                scores[i] = scores[i] - growth * 100


    good_score = 0
    bad_score = 0

    for n in range(len(scores)):
        if a[n] == 1:   #投票
            good_score = good_score + scores[n]
        else:
            bad_score = bad_score + scores[n]

    result.append((good_score/a.count(1))/(good_score/a.count(1)+bad_score/a.count(0)))

    for i in range(1,round):
        for i in range(people):
            if a[i] == 1:  # 投票
                rate[i] = rate[i] + growth
                if scores[i] + growth * 100 <= 1:
                    scores[i] = scores[i] + growth * 100
            else:
                rate[i] = rate[i] - growth
                if scores[i] -growth * 100 >=0:
                    scores[i] = scores[i] - growth * 100

        a = init(people,rate)   #投票

        good_score = 0
        bad_score = 0

        for n in range(len(scores)):
            if a[n] == 1:  # 投票
                good_score = good_score + scores[n]
            else:
                bad_score = bad_score + scores[n]

        result.append((good_score / a.count(1)) / (good_score / a.count(1) + bad_score / a.count(0)))


    return result

if __name__ == '__main__':
    Temptation = 0.6   #投票率
    score = 100
    people = 1000
    round = 301
    growth = 0.01
    a = vote(Temptation, people, round, growth, score)
    print(a)
